
import Modules.Login.Login;
import Modules.Login.LoginInfo;
import Modules.PostLogin;
import Modules.Setup.InitialSetupWindow;

import com.desidea.common.AppXMLLoader;
import com.desidea.common.ApplicationInfo;
import com.desidea.common.CommonClass;
import com.desidea.common.SFASEvent;

import flash.display.Screen;

import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.utils.StringUtil;

/****************************************************************************************
 * DECLARATIONS
 * **************************************************************************************/
private var _usePreLogin:Boolean = true;
 
/****************************************************************************************
 * EVENT HANDLER METHODS
 * **************************************************************************************/
private function appPreInitialize():void{
	LoadLocalConfiguration();
}

private function LoadLocalConfiguration():void{
	try{
		var _appXml:XML = AppXMLLoader.getInstance().LoadLocalXML();
		CommonClass.ServiceURL = String(_appXml.serviceurl);
		
//		CommonClass.updateConfigFile = AppXMLLoader.getInstance().getAutoUpdateConfigFile();
	}catch(er:Error){
		Alert.show("Unable to load local configuration file. \n" + er.message);
	}
}

private function WinAppInitialized():void{
	if(StringUtil.trim(CommonClass.ServiceURL).length > 0){
		this.enabled = false;
	}else{
		_usePreLogin = false;
	}
}

private function WinAppCreated():void{
	if(_usePreLogin){
		this.displayLogin();
	}else{
		displaySetup();
	}
	this.setPosition();
	this.status = ApplicationInfo.appVersion;
}

private function LoginSuccessfulEventHandler(event:ResultEvent):void{
	ProcessLogin( LoginInfo(event.result) );	
}

//private function OnPreLoginXMLLoaded(event:Event):void{
//	var ch:ConfigHelper = ConfigHelper.GetInstance( AppXMLLoader.getInstance().ConfigXML);
//	CommonClass.GlobalConfig = ch;
//	this.enabled = true;
//	if(StringUtil.trim(CommonClass.ServiceURL).length > 0){
//		CommonClass.updateConfigFile = AppXMLLoader.getInstance().getAutoUpdateConfigFile();
//		displayLogin();
//	}
//	
//	this.setPosition();
//	this.maximize();
//	///Autoupdate
//	var au:AutoUpdater = new AutoUpdater();
//	au.checkForUpdates(CommonClass.updateConfigFile);
//}

/****************************************************************************************
 * PRIVATE METHODS
 * **************************************************************************************/
private function setPosition():void{
	this.nativeWindow.x = Screen.mainScreen.bounds.width/2 - this.width/2;
	this.nativeWindow.y = Screen.mainScreen.bounds.height/2 - this.height/2;
}
private function displaySetup():void{
	var ctlSetup:InitialSetupWindow = new InitialSetupWindow();
	this.ModuleCvs.removeAllChildren();
	this.ModuleCvs.addChild(ctlSetup);
}

public function displayLogin():void{
	var ctlLogin:Login = new Login();
	ctlLogin.addEventListener(SFASEvent.LOGON_SUCCESSFUL_EVENT, LoginSuccessfulEventHandler);
	
	this.ModuleCvs.removeAllChildren();
	this.ModuleCvs.addChild(ctlLogin);
	this.enabled = true;

}

private function ProcessLogin(loginInfo:LoginInfo):void{
	this.ModuleCvs.removeAllChildren();
//	var topMenu:XML = new XML(loginInfo.NavigationXML);
	
//	CommonClass.NavigationXML = topMenu
	CommonClass.UserID = loginInfo.UserId;
	CommonClass.UserName = loginInfo.FirstName + ' ' + loginInfo.LastName;
	CommonClass.UserLoginInfo = loginInfo;
	ShowPostLogin();
}

private function ShowPostLogin():void{
	
	this.ModuleCvs.removeAllChildren();
	
	var pl:PostLogin = new PostLogin();
	pl.percentHeight = 100;
	pl.percentWidth = 100;
	
	this.ModuleCvs.addChild( pl );
}

public function DoSignOut():void{
	
	CommonClass.UserID = "";
	CommonClass.UserName = "";
	CommonClass.UserLoginInfo = null;
	CommonClass.SelectedMenuCode = "";
	CommonClass.NavigationXML = null;
	CommonClass.AuthenticatedLoginXML = null;
	
	this.displayLogin();	
}